﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;
using ZF.BLL;

namespace ZF.WS
{
    public partial class Register :BLL.Page.PageBase
    {
 
        public int IsMedium { get; set; }


        private int _zj = 0;
        [PageArgument(Key = "zj", DefaultValue = 0)]
        public int ZJ
        {
            get { return _zj; }
            set
            {
                if (value > 0 && value <= 4)
                {
                    IsMedium = 1;
                    _zj = value;
                }

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var cm = SiteInfo.GetCurrentMember();
                if (ZJ > 0 && cm != null &&!cm.IsExpert)
                {
                    Response.Redirect("/Person/expert/applye.aspx?ot=AddNew");
                    return;
                    
                }



                InitControls();



            }
        }
        public void InitControls()
        {
            this.phIsMedium.Visible = this.IsMedium == 1;
            this.ddlEType.DataSource=DictionaryConfig.EExpertType;
            this.ddlEType.DataBind();
            this.ddlEType.SelectedValue = ZJ.ToString();
            var sp=new house.Search();
            this.ddlArea.DataSource = sp.City.Select(p => new {Key=p.Name,Text=p.Name }); ;
            this.ddlArea.DataBind();
        }
        protected void _Register(object sender, EventArgs e)
        {
            try
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {

                    if (!SiteInfo.ValidateCode(this.tbVCode.Text))
                    {
                        WarnFactory.CreateWarn(new PageWarnParameter()
                        {
                            JscriptMode = true,
                            Link = WarnLink.GoBack,
                            Message = "验证码错误",
                        }).Alert();
                        return;
                    }
                    var member = dc.US_Member.FirstOrDefault(p => p.Account == this.tbUserName.Text.Trim());
                    if (member != null)
                        throw WarnFactory.CreateWarn("该用户名已经存在，请使用其他用户名", WarnIcon.Error, WarnLink.GoBack);
                    member = dc.US_Member.FirstOrDefault(p => p.Email == this.tbEmail.Text.Trim());
                    if (member != null)
                        throw WarnFactory.CreateWarn("该邮箱已经被注册使用，请使用其他邮箱", WarnIcon.Error, WarnLink.GoBack);
                    member = new ZF.Model.US_Member()
                    {
                        Account = this.tbUserName.Text.Trim(),
                        Email = this.tbEmail.Text.Trim(),
                        Pwd = SiteInfo.EncryptBase64(this.tbPassword.Text),
                        Address = this.tbAddress.Text.EmptyString(),
                        AddTime = DateTime.Now,
                        Answer = this.tbAnswer.Text.EmptyString(),
                        Contact = this.tbContact.Text.EmptyString(),
                        IDCard = this.tbIDCard.Text.EmptyString(),
                        Phone = this.tbPhone.Text.EmptyString(),
                        Professional = this.ddlJob.SelectedValue.EmptyString(),
                        QQ = this.tbQQ.Text.EmptyString(),
                        Question = this.tbQuestion.Text.EmptyString(),
                        Sex = this.rblSex.SelectedValue.Convert<int>(),
                        Tel = this.tbTel.Text.EmptyString(),
                        Zipcode = this.tbZipcode.Text.EmptyString(),
                        Birthday = this.tbBD.Text,
                        Status = 1,
                        Type = 1,
                        Area = this.ddlArea.SelectedValue,
                        IsMedium = this.IsMedium == 1,
                        Company = this.tbCompany.Text.EmptyString(),
                        ExpertType = this.ddlEType.SelectedValue.Convert<int>(),
                        IsExpert = ZJ > 0,
                        ImgBusCard = this.regUpdateImg1.tbBusImg.Text.EmptyString(),
                        ImgIDCard = this.regUpdateImg1.tbIDCardImg.Text.EmptyString(),
                        ImgLicence = this.regUpdateImg1.tbLicImg.Text.EmptyString(),
                    };
                    var success = SiteInfo.RegisterMember(member, this.tbRecommend.Text);
                    var gate = new BLL.Gate.GeneralGate();
                    string msg = "";
                    gate.Login(member.Account, this.tbPassword.Text, out msg);
                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        DoPage = "/Person/index.aspx",
                        Message = "恭喜你注册成功！",
                    }).Alert();
                }
            }
            catch (Exception ex)
            {
                throw WarnFactory.CreateWarn(ex);
            }
        }


        public string GetTitle()
        {
           var expertType= DictionaryConfig.EExpertType.FirstOrDefault(p=>p.Key==this.ZJ);
           if (expertType == null)
               return "新会员";
           else
               return expertType.Text;
 
        }
    }
}
